package traditionalthread;

public class SynchronizedStatic implements Runnable {

	private static boolean flag = true;
	
	//锁是Synchronizedstatic.class这个类
	public static synchronized void test1(){
		for(int i=1;i<=100;i++){
			System.out.println("test1 "+Thread.currentThread().getName()+" "+i);
		}
	}
	//锁是创建的对象，即下面的ss，当把test2的锁变为Synchronizedstatic.class时执行时会一次执行，用this时会交替执行
	public void test2(){
		synchronized (this) {
			for(int i=1;i<=100;i++){
				System.out.println("test1 "+Thread.currentThread().getName()+" "+i);
			}
		}
	}
	public static void main(String[] args) throws InterruptedException {
		SynchronizedStatic ss = new SynchronizedStatic();
		new Thread(ss).start();
		new Thread(ss).start();

	}

	public void run() {
		if(SynchronizedStatic.flag==true){
			SynchronizedStatic.flag=false;
			test1();
		}else{
			SynchronizedStatic.flag=true;
			test2();
		}
		
	}

}
